package acceptanceTests.deleteAmessage;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.Socket;
import server.ForumServer;
import server.WSEPHandler;
import domain.items.ForumThread;
import domain.items.Item;
import domain.items.Post;


public class DeleteMessageImp implements DeleteMessageInt {
	private boolean _implementedDeleteMessage  = true;
	private boolean _implementedGetLegalTopicForNewMessage  = true;
	private boolean _implementedGetMessageTopic  = false;
	private boolean _implementedHasMessage  = true;
	private boolean _implementedWriteNewMessageReturnsMessage  = true;
	private boolean _implementedPremoteUser  = true;
	private boolean _implementedRegister  = true;
	
	private ForumServer _server;
	private WSEPHandler _handler;
	

	public DeleteMessageImp(ForumServer server){
		this._server = server;
		try{
			Socket s= new Socket("localhost", getPort());
			_handler= new WSEPHandler(s,server);
		}
		catch(IOException e){
		}
	}
	
	private static int getPort() {
		// TODO Auto-generated method stub
		String str="0";
		try {
	        BufferedReader in = new BufferedReader(new FileReader("port.txt"));
	        str = in.readLine();
	        in.close();
	    } catch (IOException e) {
	    }
		return Integer.parseInt(str);
	}



	public boolean is_implementedGetLegalTopicForNewMessage() {
		return _implementedGetLegalTopicForNewMessage;
	}

	public void set_implementedGetLegalTopicForNewMessage(
			boolean getLegalTopicForNewMessage) {
		_implementedGetLegalTopicForNewMessage = getLegalTopicForNewMessage;
	}

	public boolean is_implementedGetMessageTopic() {
		return _implementedGetMessageTopic;
	}

	public void set_implementedGetMessageTopic(boolean getMessageTopic) {
		_implementedGetMessageTopic = getMessageTopic;
	}

	public boolean is_implementedHasMessage() {
		return _implementedHasMessage;
	}

	public void set_implementedHasMessage(boolean hasMessage) {
		_implementedHasMessage = hasMessage;
	}

	public boolean is_implementedWriteNewMessageReturnsMessage() {
		return _implementedWriteNewMessageReturnsMessage;
	}

	public void set_implementedWriteNewMessageReturnsMessage(
			boolean writeNewMessageReturnsMessage) {
		_implementedWriteNewMessageReturnsMessage = writeNewMessageReturnsMessage;
	}


	public boolean deleteMessage(Object message) {
		Item i1;
		ForumThread t1;
		_handler.getRootTopic();
		_handler.setCurrentlyViewedItem(_server.getDataRepository().getRootTopic());
		_handler.getItem(1);
		_handler.getItem(1);
		i1=_handler.get_currentlyViewedItem();
		System.out.println(i1.getName());
		t1= (ForumThread)i1;
		System.out.println(t1.getName() + " size: " + t1.getPosts().size());
		_handler.getItem(t1.getPosts().size());

		try{
		_handler.deletePost();
		}
		catch(ClassCastException e){}
		
		return true;
	}

	public boolean is_implementedDeleteMessage() {
		return _implementedDeleteMessage;
	}

	public void set_implementedDeleteMessage(boolean deleteMessage) {
		_implementedDeleteMessage = deleteMessage;
	}

	
	public Object getLegalTopicForNewMessage(String userName) {
		_handler.getRootTopic();
		_handler.setCurrentlyViewedItem(_server.getDataRepository().getRootTopic());
		_handler.getItem(1);
		String s1= _handler.getItem(1);
		//System.out.println("s = " + s1 );
		
		return s1;
	}

	
	public Object getMessageTopic(Object message) {
		// TODO Auto-generated method stub
		return null;
	}

	
	public boolean hasMessage(Object message, String userName) {
		Post p= (Post)message;
		ForumThread t=(ForumThread)_handler.get_currentlyViewedItem().getFather();
		
		for (int i = 0; i < t.getPosts().size(); i++) {
			Post currentPost= t.getPosts().elementAt(i);
			if(currentPost.getName().equals(p.getName())){
				return true;
			}
		}
		return false;
	}

	
	public Object writeNewMessageReturnsMessage(String subject, String body,
			Object topic, String userName) {
			Post p= new Post(_handler.get_currentlyViewedItem(),userName, subject, body);
			_handler.addPost(subject, body, userName);
			_handler.getItem(1);
			return p;
	}

	
	public boolean premoteUser(String username) {
		_handler.promoteUser(username);
		return true;
	}

	public boolean is_implementedPremoteUser() {
		return _implementedPremoteUser;
	}

	public void set_implementedPremoteUser(boolean premoteUser) {
		_implementedPremoteUser = premoteUser;
	}

	
	public boolean register(String userName, String email, String password) {
		_handler.registerUser(userName, email, password);
		return true;
	}

	public boolean is_implementedRegister() {
		return _implementedRegister;
	}

	public void set_implementedRegister(boolean register) {
		_implementedRegister = register;
	}

}
